package cn.txun.csmall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration
public class RedisConfiguration {

    @Bean                     //在本方法参数上声明连接redis的工厂
    public RedisTemplate<String, Serializable> redisTemplate(
            RedisConnectionFactory connectionFactory){
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //设置redis连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        //设置key的String序列化器：将字符串序列化，存入redis
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //设置value的JSON序列化器：将对象先转为JSON，然后序列化，存入redis
        redisTemplate.setValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }



}
